package tt.dz.dao.ext.system;

import java.util.List;

import org.apache.commons.collections.map.ListOrderedMap;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.system.OmsRole;
import tt.dz.util.Constants;

/**
 * 角色数据操作dao
 * @author Administrator
 * @date 2015年12月16日
 */
@Repository
public class OmsRoleDao extends OmsCommonDao<OmsRole> {

	/**
	 * 分页查询有效角色信息
	 * @author Administrator
	 * @param paramsMap 有序参数Map
	 * @param pageNo 页码索引
	 * @param pageSize 每页个数
	 * @param sortName 排序列名
	 * @param sortVal 排序方式 1 正序 其他倒序
	 * @return
	 */
	public List<OmsRole> pagingFindValidRoleEmbedList(
			ListOrderedMap paramsMap, Integer pageNo, Integer pageSize, String sortName,
			Integer sortValue){
		
		if (pageNo!=null && pageNo <= 1) {
			pageNo = 1;
		}
		// 如果参数集没有初始化
		if (paramsMap == null) {
			paramsMap = new ListOrderedMap();
		}
		// 设置有效标志
		paramsMap.put(Constants.Jdbc.VALID_FLAG, 1);
		
		return super.findListByClass(OmsRole.class, paramsMap, pageNo, pageSize, sortName, sortValue);
	}
	
	/**
	 * 通过roleid逻辑删除操作员
	 * @author Administrator
	 * @param role_id
	 * @return
	 */
	public int logicDeleteRoleByRoleId(String role_id){
		
		OmsRole role = new OmsRole();
		//设置参数
		role.setRole_id(role_id);
		return super.LogicDeleteObject(role);
	}
	
	public List<OmsRole> findRolesByEntityType(Integer entity[]) throws OmsDaoException
	{
		StringBuffer sb = new StringBuffer();
		sb.append("select * from oms_role where entity_type in (");
		for (int i = 0;i<entity.length;i++ )
		{
			if (i > 0)
			{
				sb.append(",");
			}
			sb.append("?");
		};
		sb.append(")");
		try
		{
			return getJdbcTemplate().query(sb.toString(),entity,new BeanPropertyRowMapper<>(OmsRole.class));
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e);
		}
	}

	public JsonForList<?> findExistRolesByOperId(String operId) {
//		List<OmsRole>  list  =   findListByCondition(new OmsRole(), "where '"+operId+"'=");
		List<OmsRole>  list  =   findDataBySqlOfClass("SELECT role.role_id,role.role_name,role.entity_type,role.entity_id,role.entity_name,role.valid_flag,role.create_time,role.create_oper_id,role.update_time, role.update_oper_id "
				+ "from oms_role role,oms_oper_role oor where role.role_id = oor.role_id and oor.oper_id = ?", OmsRole.class, operId);
//		List<OmsRole>  list1  =   findDataBySqlOfClass("SELECT role.role_id,role.role_name,role.entity_type,role.entity_id,role.entity_name,role.valid_flag,role.create_time,role.create_oper_id,role.update_time, role.update_oper_id "
//				+ "from oms_role role,oms_oper_role oor where role.role_id = oor.role_id and oor.oper_id = '"+operId+"'", OmsRole.class, "");
		
//		return null;
		JsonForList<OmsRole> listJson = new JsonForList<OmsRole>();
		listJson.setList(list);
		listJson.setCode("200");
		return listJson;
		
	}

	public JsonForList<?> findCanbindRolesByOperId(String operId,String entityId,String entityType) {
		List<OmsRole>  list  =   findDataBySqlOfClass("SELECT role_id,role_name,entity_type,entity_id,entity_name,valid_flag,"
				+ "create_time,create_oper_id,update_time, update_oper_id from oms_role role where valid_flag = 1 and entity_id = ? "
				+ "and entity_type = ? and role_id not in (SELECT role.role_id from oms_role role,oms_oper_role oor "
				+ "where role.role_id = oor.role_id and oor.oper_id = ?)", OmsRole.class, entityId,entityType,operId);
		JsonForList<OmsRole> listJson = new JsonForList<OmsRole>();
		listJson.setList(list);
		listJson.setCode("200");
		return listJson;
	}
}